|
|
|
| Table 19.3 Visual Basic's Built-In Color Constants | | Constant | Numeric Value (Decimal) | | vbBlack | 0 | | vbRed | 255 | | vbGreen | 65280 | | vbYellow | 65535 | | vbBlue | 16711680 | | vbMagenta | 16711935 | | vbCyan | 16776960 | | vbWhite | 16777215 |
|
|
|
|
|
|
|
System color constants are also available. For example, the following statement sets the color of a form to the same color as the Windows desktop: |
|
|
|
|
|
|
|
|
|
|
Form1.BackColor = vbDesktop n |
|
|
|
|
|
|
|
|
The color constants use such unusual numbers because each number represents the levels of red, green, and blue in a given color. If you want to create a custom color and know how much of each primary color to include, you can find out the color number by using the RGB function. Each primary color can have a setting between 0 and 255. For example, the following code determines the number for the color gray (equal parts red, green, and blue): |
|
|
|
|
|
|
|
|
Dim lNewColor As Long
lNewColor = RGB(192,192,192)
Form1.BackColor = lNewColor |
|
|
|
|
|
|
|
|
|
An easy way to find out a color number for use in code is to select the color from the Color list and make a note of the number displayed in the Properties window.  |
|
|
|
|
|
|
|
|
|
|
Another useful color function is the QBColor function. In earlier versions of BASIC, such as QuickBasic and GWBasic for MS-DOS, consecutive integer numbers represented colors. The QBColor function returns the hexadecimal RGB equivalent color. This capability is useful if you want to generate random colors, as in the following example: |
|
|
|
|
|
|
|
|
Private Sub Form_Click()
Dim lRGBColor As Long
Dim nIntColor As Integer
'Choose random number between 0 and 15
nIntColor = Int(16 * Rnd)
lRGBColor = QBColor(nIntColor)
Me.BackColor = lRGBColor
End Sub |
|
|
|
|
|